package com.yserp.project.erp.order.productorder.controller;

import com.yserp.common.utils.poi.ExcelUtil;
import com.yserp.common.utils.text.Convert;
import com.yserp.framework.aspectj.lang.annotation.Log;
import com.yserp.framework.aspectj.lang.enums.BusinessType;
import com.yserp.framework.web.controller.BaseController;
import com.yserp.framework.web.domain.AjaxResult;
import com.yserp.framework.web.page.TableDataInfo;
import com.yserp.project.erp.order.productorder.domain.ProductionOrderItem;
import com.yserp.project.erp.order.productorder.service.IProductionOrderItemService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

/**
 * 商品入库单子Controller
 *
 * @author yserp
 * @date 2022-07-19
 */
@Controller
@RequestMapping("/order/productorder/item")
public class ProductionOrderItemController extends BaseController {
    private String prefix = "order/productorder/item";

    @Autowired
    private IProductionOrderItemService productionOrderItemService;

    @RequiresPermissions("order:productorder:view")
    @GetMapping()
    public String productorder() {
        return prefix + "/productorder";
    }

    /**
     * 查询商品入库单子列表
     */
    @RequiresPermissions("order:productorder:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProductionOrderItem productionOrderItem) {
        startPage();
        List<ProductionOrderItem> list = productionOrderItemService.selectProductionOrderItemList(productionOrderItem);
        return getDataTable(list);
    }

    /**
     * 导出商品入库单子列表
     */
    @RequiresPermissions("order:productorder:export")
    @Log(title = "商品入库单子", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProductionOrderItem productionOrderItem) {
        List<ProductionOrderItem> list = productionOrderItemService.selectProductionOrderItemList(productionOrderItem);
        ExcelUtil<ProductionOrderItem> util = new ExcelUtil<ProductionOrderItem>(ProductionOrderItem.class);
        return util.exportExcel(list, "商品入库单子数据");
    }

    /**
     * 新增商品入库单子
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存商品入库单子
     */
    @RequiresPermissions("order:productorder:add")
    @Log(title = "商品入库单子", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProductionOrderItem productionOrderItem) {
        return toAjax(productionOrderItemService.save(productionOrderItem));
    }

    /**
     * 修改商品入库单子
     */
    @RequiresPermissions("order:productorder:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        ProductionOrderItem productionOrderItem = productionOrderItemService.getById(id);
        mmap.put("productionOrderItem", productionOrderItem);
        return prefix + "/edit";
    }

    /**
     * 修改保存商品入库单子
     */
    @RequiresPermissions("order:productorder:edit")
    @Log(title = "商品入库单子", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProductionOrderItem productionOrderItem) {
        return toAjax(productionOrderItemService.updateById(productionOrderItem));
    }

    /**
     * 删除商品入库单子
     */
    @RequiresPermissions("order:productorder:remove")
    @Log(title = "商品入库单子", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(productionOrderItemService.removeByIds(Arrays.asList(Convert.toLongArray(ids))));
    }

    /**
     * 查询子表列表
     */
    @PostMapping("/ajaxList")
    @ResponseBody
    public AjaxResult ajaxList(ProductionOrderItem productionOrderItem) {
        List<ProductionOrderItem> list = productionOrderItemService.selectProductionOrderItemList(productionOrderItem);
        return AjaxResult.success(list);
    }


}
